package com.luhuiguo.cms.server.config;

import static freemarker.core.Configurable.DATETIME_FORMAT_KEY;
import static freemarker.core.Configurable.DATE_FORMAT_KEY;
import static freemarker.core.Configurable.NUMBER_FORMAT_KEY;
import static freemarker.core.Configurable.TIME_FORMAT_KEY;
import static freemarker.core.Configurable.URL_ESCAPING_CHARSET_KEY;
import static freemarker.template.Configuration.AUTO_INCLUDE_KEY;
import static freemarker.template.Configuration.LOCALIZED_LOOKUP_KEY;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import javax.inject.Inject;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfig;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import com.luhuiguo.cms.server.service.CmsService;
import com.luhuiguo.cms.server.util.PathUtils;
import com.luhuiguo.cms.server.web.ChannelHandler;
import com.luhuiguo.cms.server.web.SiteController;
import com.luhuiguo.cms.server.web.template.FreeMarkerTemplateViewResolver;
import com.luhuiguo.cms.server.web.template.VfsTemplateLoader;

import freemarker.cache.TemplateLoader;
import freemarker.template.TemplateException;

/**
 * The spring java-based configuration for Spring-MVC.
 * 
 * @author LuHuiguo
 * @version $Id: SiteConfig.java 181 2010-05-24 01:16:48Z luhuiguo@gmail.com $
 */
@Configuration
public class SiteConfig {

    @Inject
    private CmsService cmsService;

    @Inject
    private ChannelHandler channelHandler;

    @Bean
    public TemplateLoader vfsTemplateLoader() {

        return new VfsTemplateLoader("/templates/");
    }

    @Bean
    public HandlerMapping handlerMapping() {

        DefaultAnnotationHandlerMapping handlerMapping = new DefaultAnnotationHandlerMapping();

        handlerMapping.setDefaultHandler(channelHandler);

        return handlerMapping;
    }

    @Bean
    public FreeMarkerConfig freemarkerConfig() throws IOException, TemplateException {
        FreeMarkerConfigurer cfg = new FreeMarkerConfigurer();
        cfg.setDefaultEncoding("UTF-8");
        Properties settings = new Properties();

        settings.setProperty(LOCALIZED_LOOKUP_KEY, "false");
        settings.setProperty(NUMBER_FORMAT_KEY, "#");
        settings.setProperty(DATE_FORMAT_KEY, "yyyy-MM-dd");
        settings.setProperty(TIME_FORMAT_KEY, "HH:mm:ss");
        settings.setProperty(DATETIME_FORMAT_KEY, "yyyy-MM-dd HH:mm:ss");
        settings.setProperty(AUTO_INCLUDE_KEY, "commons.ftl");
        settings.setProperty(URL_ESCAPING_CHARSET_KEY, "UTF-8");
        cfg.setFreemarkerSettings(settings);

        Map<String, Object> variables = new HashMap<String, Object>();
        variables.put("cms", cmsService);
        cfg.setFreemarkerVariables(variables);
        cfg.setTemplateLoaderPaths(new String[]{ "/WEB-INF/templates/", "/templates/" });
        cfg.setPreTemplateLoaders(new TemplateLoader[]{ vfsTemplateLoader() });
        return cfg;
    }

    @Bean
    public ViewResolver freemarkerViewResolver() {
        FreeMarkerTemplateViewResolver viewResolver = new FreeMarkerTemplateViewResolver();
        viewResolver.setContentType("text/html;charset=UTF-8");
        viewResolver.setCache(true);
        viewResolver.setPrefix("");
        viewResolver.setSuffix(PathUtils.TPL_SUFFIX);
        // viewResolver.setOrder(2);
        return viewResolver;
    }

    @Bean
    public SiteController siteController() {
        return new SiteController();
    }

    @Bean
    public ChannelHandler channelHandler() {
        return new ChannelHandler();
    }

}
